<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\property\basic;

use app\common\repositories\BaseRepository;
use app\common\dao\property\basic\PropertyLabelDao;
use app\common\repositories\property\users\PropertyUsersLogRepository;
use think\facade\Db;

/**
 * Class PropertyLabelRepository
 *
 * @mixin PropertyLabelDao
 */
class PropertyLabelRepository extends BaseRepository
{

    public function __construct(PropertyLabelDao $dao)
    {
        $this->dao = $dao;
    }


    public function getPropertyList(array $where, $page, $limit)
    {
        $query = $this->dao->search($where);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->order('id', 'desc')
            ->select();
        return compact('count', 'list');
    }

    /**
     * 根据条件查全部
     * @param array $where
     * @param $propertyId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-12 11:22
     */
    public function getApiAllList(array $where)
    {
        return $this->dao->getAll($where['is_show'], 'id,label_name')->toArray();
    }

    public function addInfo(array $data, int $userId = 0)
    {
        return $this->dao->create($data);
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return $this->dao->update($info['id'], $data);
    }


    public function getDetail(int $id)
    {
        $data = $this->dao->search([])
            ->where('id', $id)
            ->find();
        return $data;
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        $this->dao->delete($info['id']);
    }

    public function getLabelData(array $where = [])
    {
        $list = $this->dao->search($where)
            ->field('label_name label,id value')
            ->select()->toArray();
        return $list;
    }
}